package com.ebupt.migu.fineoperationactivitymonitor.common.exception;

/*
 * com.ebupt.migu.music.common.exception Author：zhanggk Date：2018-12-28
 */

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ebupt.migu.fineoperationactivitymonitor.common.entity.ResultObject;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * WebExceptionHandler
 * 
 * @Author zhanggk
 * @Date 2018-12-28
 */
@ControllerAdvice
@Slf4j
public class WebExceptionHandler {

    /**
     * 业务用ControllerAdvice
     * 
     * @param e 异常
     * @return ResultObject
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultObject<String> jsonErrorHandler(Exception e) {
        ResultObject<String> result = new ResultObject<>();
        if (e instanceof MyException) {
            log.error("业务异常：[{}]", e.getMessage());
            return getStringResultInfo(e.getMessage(), ((MyException)e).getStatusEnum(), result);
        }
        if (e instanceof PromptException) {
            log.error("业务异常：[{}]", e.getMessage());
            return getStringResultInfo(e.getMessage(), ((PromptException)e).getStatusEnum(), result);
        }
        log.error("系统异常", e);
        return getStringResultInfo(e.getMessage(), StatusEnum.ERROR, result);
    }

    /**
     * 统一状态处理
     * 
     * @param se StatusEnum
     * @param result ResultObject
     * @return ResultObject
     */
    private ResultObject<String> getStringResultInfo(String msg, StatusEnum se, ResultObject<String> result) {
        result.setCode(se.getCode());
        result.setMsg(se.getMsg());
        result.setData(msg);
        return result;
    }
}
